﻿using System;
using System.ComponentModel;

namespace System.Erp.Runtime
{
    /// <summary>
    /// IPartProviderAttribute的实现Attribute标记的位置，本来用Type足够描述，但设计此类的目的是加快扫描一些内置Attribute的速度。
    /// </summary>
    public sealed class AttributeReflectedSource
    {
        private readonly Type _type;
        /// <summary>
        /// IPartProviderAttribute的实现Attribute标记的类
        /// </summary>
        public Type Type
        {
            get { return _type; }
        }

        /// <summary>
        /// 创建 AttributeReflectedSource 实例
        /// </summary>
        /// <param name="type">IPartProviderAttribute的实现Attribute标记的类</param>
        public AttributeReflectedSource(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            _type = type;

            object[] temp = type.GetCustomAttributes(false);
            Attribute[] atts = new Attribute[temp.Length];
            temp.CopyTo(atts, 0);
            _atts = new AttributeCollection(atts);

            _workspace = new Lazy<Workspace>(GetWorkspace, true);
            _creatorAttribute = new Lazy<CreatorAttribute>(GetCreatorAttribute,true);
        }

        private AttributeCollection _atts;
        /// <summary>
        /// 返回这个类所标注的所有Attribute
        /// </summary>
        public AttributeCollection Attributes
        {
            get { return _atts; }
        }

        private readonly Lazy<Workspace> _workspace;
        /// <summary>
        /// 返回声明此Attribute的类上标注的Workspace。
        /// </summary>
        public Workspace Workspace
        {
            get { return _workspace.Value; }
        }

        private Workspace GetWorkspace()
        {
            Attribute att = this.Attributes[typeof(WorkspaceAttribute)];
            if (att!=null)
            {
                return ((WorkspaceAttribute)att).Workspace;
            }
            return Workspace.Global;
        }

        private readonly Lazy<CreatorAttribute> _creatorAttribute;
        /// <summary>
        /// 如果所在的类上标记了CreatorAttrute，那么将返回定义的实例。
        /// </summary>
        public CreatorAttribute CreatorAttribute
        {
            get { return _creatorAttribute.Value; }
        }

        private CreatorAttribute GetCreatorAttribute()
        {
            Attribute att = this.Attributes[typeof(CreatorAttribute)];
            if (att!=null)
            {
                return ((CreatorAttribute)att);
            }
            return new CreatorAttribute();
        }

        /// <summary>
        /// 返回某个类的识别名称。
        /// </summary>
        /// <param name="type">类型，不能为空</param>
        /// <returns>类型的识别名称。</returns>
        public static string GetTypeIdentity(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            return ContractNameServices.GetTypeIdentity(type);
        }
    }
}
